﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml.Schema;
using Driven.ProcessScheduler.Conversion;
using NUnit.Framework;
using Rhino.Mocks;

namespace Driven.ProcessScheduler.Test.Conversion
{
    [TestFixture]
    public class XmlScheduleConvertorTest
    {
        [Test]
        public void CanRepresentSchedulesAsXml()
        {
            ScheduleManager scheduleManager = createScheduleManager();

            IScheduleConvertor scheduleConvertor = new XmlScheduleConvertor();

            string xml = scheduleConvertor.Convert(scheduleManager);

            XDocument doc = XDocument.Parse(xml);

            var schemas = new XmlSchemaSet();
            schemas.Add("", "processes.xsd");

            doc.Validate(schemas, (o, e) => Assert.Fail(e.Message));

        }

        private ScheduleManager createScheduleManager()
        {
            var component = MockRepository.GenerateStub<IScheduledComponent>();
            component.Expect(c => c.ToString()).Return("Stub Component").Repeat.Any();
            var schedule1 = MockRepository.GenerateStub<Schedule>("Schedule A", component, TimeSpan.Zero, 0);
            var schedule2 = MockRepository.GenerateStub<Schedule>("Schedule B", component, TimeSpan.Zero, 0);

            return new ScheduleManager(new List<Schedule> { schedule1, schedule2 });
        }
    }
}
